#include "pagetraction.h"
#include "ui_pagetraction.h"

PageTraction::PageTraction(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageTraction)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    m_lTracStatNameList<<"牵引系统输入电流(A)"
                       <<"牵引系统输入电压(V)"
                       <<"牵引逆变器温度(°C)"
                       <<"牵引力完成值(KN)"
                       <<"电制动力完成值(KN)"
                       <<"牵引电机温度(°C)";
    m_lTracStatCntOfOneCar<<1
                          <<1
                          <<1
                          <<1
                          <<1
                          <<1;
    InitTracStatLabels();
    setDefaultStat();
}

PageTraction::~PageTraction()
{
    delete ui;
}

void PageTraction::setDefaultStat()
{
    for(int i=0;i<4;i++)
    {
        setTracStat(0,1+i,0);
        setTracStat(1,1+i,0);
        setTracStat(2,1+i,0);
        setTracStat(3,1+i,0);
        setTracStat(4,1+i,0);
        setTracStat(5,1+i,0);
    }
}

void PageTraction::InitTracStatLabels()
{
    m_nStatLabelWidth_Car = MiniCarSaloonWidth+MiniCarHSpace_1220;
    m_nStatLabelSpaceX = MiniCarLeftSpace_1220 - MiniCarHSpace_1220/2;
    m_nStatLabelHeight = 55;
    m_nStatLabelStartY = 15;
    m_nStatLabelSpaceY = 0;

    uint8_t statCnt = m_lTracStatNameList.length();
    for(int i=0;i<statCnt;i++)
    {
        QLabel *statName = new QLabel(this);
        statName->setStyleSheet(StatLabelStyle);
        statName->setAlignment(Qt::AlignCenter);
        statName->setText(m_lTracStatNameList.at(i));
        statName->setGeometry(m_nStatLabelSpaceX-190,
                              m_nStatLabelStartY+i*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                              190,m_nStatLabelHeight);

        uint8_t statOneCarCnt = m_lTracStatCntOfOneCar.at(i);
        QList<QLabel*> OneStatList;
        for(int j=0;j<SaloonCnt;j++)
        {
            for(int k=0;k<statOneCarCnt;k++)
            {
                QLabel *stat = new QLabel(this);
                stat->setStyleSheet(StatLabelStyle);
                stat->setAlignment(Qt::AlignCenter);
                SetPosition(stat,CAR(j),ROW(i),COL(k),TOTAL(statOneCarCnt),j>1);
                OneStatList.append(stat);
            }
        }
        m_lTracStatList.append(OneStatList);
    }
}

//row 和 col 都从0开始
void PageTraction::SetPosition(QWidget* w,int car, uint8_t row, uint8_t col, uint8_t total, bool xReverse)
{
    float OnestatLabelWidth = m_nStatLabelWidth_Car/total;
//    float rowSpaceX = (m_nStatLabelWidth_Car - total*OnestatLabelWidth)/(total+1);
    float rowSpaceX = 0;
    if(!xReverse)
        w->setGeometry(m_nStatLabelSpaceX+rowSpaceX+car*m_nStatLabelWidth_Car+col*(OnestatLabelWidth+rowSpaceX),
                       m_nStatLabelStartY+row*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                       OnestatLabelWidth,m_nStatLabelHeight);
    else
        w->setGeometry(m_nStatLabelSpaceX+rowSpaceX+car*m_nStatLabelWidth_Car+(total-1-col)*(OnestatLabelWidth+rowSpaceX),
                       m_nStatLabelStartY+row*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                       OnestatLabelWidth,m_nStatLabelHeight);
};

void PageTraction::setTracStat(uint8_t item,uint8_t index,int16_t value)
{
    if(index >= m_lTracStatList[item].length())
        return;
    if(item == 0 || item == 1)
    {
        if(value == 0x7FFF)
            m_lTracStatList[item][index]->setText("?");
        else
            m_lTracStatList[item][index]->setText(QString("%1").arg(value));
    }
    else if(item == 2)
    {
        uint8_t temp = value;
        if(temp == 0xFF)
            m_lTracStatList[item][index]->setText("?");
        else
            m_lTracStatList[item][index]->setText(QString("%1").arg(temp));
    }
    else if(item == 3 || item == 4)
    {
        uint16_t force_value = value;
        if(force_value == 0xFFFF)
            m_lTracStatList[item][index]->setText("?");
        else
            m_lTracStatList[item][index]->setText(QString("%1").arg(force_value*0.01));
    }
    else if(item == 5)
    {
        int8_t temp = value;
        if(temp == 0x7F)
            m_lTracStatList[item][index]->setText("?");
        else
            m_lTracStatList[item][index]->setText(QString("%1").arg(temp*2));
    }
}
